'use strict';

const conn = require('../../libs/Conn');

module.exports = async function (params) {
    let orderBySql = ``;
    /*排序*/
    if(params.sort&&params.dir){
        switch (params.sort) {
            case 'unit_type':
                orderBySql = `ORDER BY qcf.unit_type ${params.dir}`;
                break;
            case 'check_result':
                orderBySql = `ORDER BY qcf.check_result ${params.dir}`;
                break;
            case 'check_date':
                orderBySql = `ORDER BY qcf.check_date ${params.dir}`;
                break;
            case 'user_name':
                orderBySql = `ORDER BY user_name ${params.dir}`;
                break;
        }
    }
    /*分页*/
    let pageSql = `LIMIT ${params.pageNum>1?((params.pageNum-1)*10):0},10;SELECT FOUND_ROWS() AS count`;

    let querySql = `SELECT
                        SQL_CALC_FOUND_ROWS
                        qcf.check_id,
                        qcf.unit_type,
                        qcf.check_details,
                        qcf.check_result,
                        qcf.rectification_notice,
                        qcf.rectification_reply,
                         DATE_FORMAT(
                            qcf.rectification_date,
                            '%Y-%m-%d'
                        ) AS rectification_date,
                        qcf.remarks,
                        DATE_FORMAT(qcf.check_date,'%Y-%m-%d') AS check_date,
                        qcf.check_type,
                        GROUP_CONCAT(uf.user_name SEPARATOR ',') AS user_name,
                        GROUP_CONCAT(qcum.user_id SEPARATOR ',') AS user_ids
                    FROM
                        quality_check_file qcf
                    LEFT JOIN quality_check_user_middle qcum ON qcum.check_id = qcf.check_id
                    LEFT JOIN user_file uf ON uf.user_id = qcum.user_id
                    AND uf.delete_sign = 1
                    WHERE
                        qcf.engineering_id = '${params.engineering_id}' 
                    AND qcf.check_type = '${params.check_type}'    
                    AND qcf.delete_sign = 1
                    GROUP BY check_id  ${pageSql}`;
    let rec = await conn(querySql);
    for(let data in rec[0] ){
        if(params.check_type === '1'){
            switch (rec[0][data].unit_type) {
                case 1:
                    rec[0][data].unit_name = '建设单位';
                    break;
                case 2:
                    rec[0][data].unit_name = '设计单位';
                    break;
                case 3:
                    rec[0][data].unit_name = '施工单位';
                    break;
                case 4:
                    rec[0][data].unit_name = '监理单位';
                    break;
                case 5:
                    rec[0][data].unit_name = '勘察单位';
                    break;
                case 6:
                    rec[0][data].unit_name = '图审单位';
                    break;
            }
        }
        switch (rec[0][data].check_result) {
            case 1:
                rec[0][data].check_result = '是';
                rec[0][data].check_num = 1;
                break;
            case 2:
                rec[0][data].check_result = '否';
                rec[0][data].check_num = 2;
                break;
        }
        if (rec[0][data].rectification_reply){
            rec[0][data].reply_result = '是'
        }else{
            rec[0][data].reply_result = '--'
        }
    }
    let pageNum = Math.ceil(rec[1][0].count/10);
    if(rec[0].length>0){
        rec[0][0].dataTotal = rec[1][0].count;
        rec[0][0].pageTotal = pageNum;
        rec[0][0].pageNum = params.pageNum;
    }
    return {success: true, data: rec[0]};

};